package com.gpipe.dirwatcherRealtime;

public abstract class WriterThread implements Runnable {

	/**
     * Whether or not this thread is active.
     */
    private boolean active = false;
    
    /**
     * The name of this pool (for loggin/display purposes).
     */
    protected String name;

    /**
     * This instance's thread
     */
    private Thread runner;

    /**
     * Construct a new interval thread that will run on the given name.
     *
     * @param name            the name of the thread
     */
    public WriterThread( String name) {

        this.name = name;
    }

    /**
     * Start the thread on the specified interval.
     */
    public void start() {

    	active = true;
    	
        //If we don't have a thread yet, make one and start it.
        if (runner == null) {
            runner = new Thread(this);
            runner.start();
        }
    }

    /**
     * Stop the interval thread.
     */
    public void stop() {
        
    	active = false;
    }
    

    /**
     * Not for public use.  This thread is automatically
     * started when a new instance is retrieved with the getInstance method.
     * Use the start() and stop() methods to control the thread.
     *
     * @see Thread#run()
     */
    public void run() {

        //Make this a relatively low level thread
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        //Pause this thread for the amount of the interval
        
            doWriting();
    }

    /**
     * String representation of this object.  Just the name given to it an
     * instantiation.
     *
     * @return the string name of this pool
     */
    public String toString() {
        return name;
    }

    /**
     * The interval has expired and now it's time to do something.
     */
    protected abstract void doWriting();
}

